package cn.iocoder.yudao.module.crossborder.order.service.ordermain;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.order.controller.admin.ordermain.vo.*;
import cn.iocoder.yudao.module.crossborder.order.dal.dataobject.ordermain.OrderMainDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.order.dal.mysql.ordermain.OrderMainMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.order.enums.ErrorCodeConstants.*;

/**
 * 跨境电商订单主 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OrderMainServiceImpl implements OrderMainService {

    @Resource
    private OrderMainMapper orderMainMapper;

    @Override
    public Long createOrderMain(OrderMainSaveReqVO createReqVO) {
        // 插入
        OrderMainDO orderMain = BeanUtils.toBean(createReqVO, OrderMainDO.class);
        orderMainMapper.insert(orderMain);

        // 返回
        return orderMain.getId();
    }

    @Override
    public void updateOrderMain(OrderMainSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderMainExists(updateReqVO.getId());
        // 更新
        OrderMainDO updateObj = BeanUtils.toBean(updateReqVO, OrderMainDO.class);
        orderMainMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderMain(Long id) {
        // 校验存在
        validateOrderMainExists(id);
        // 删除
        orderMainMapper.deleteById(id);
    }

    @Override
        public void deleteOrderMainListByIds(List<Long> ids) {
        // 删除
        orderMainMapper.deleteByIds(ids);
        }


    private void validateOrderMainExists(Long id) {
        if (orderMainMapper.selectById(id) == null) {
            throw exception(ORDER_MAIN_NOT_EXISTS);
        }
    }

    @Override
    public OrderMainDO getOrderMain(Long id) {
        return orderMainMapper.selectById(id);
    }

    @Override
    public PageResult<OrderMainDO> getOrderMainPage(OrderMainPageReqVO pageReqVO) {
        return orderMainMapper.selectPage(pageReqVO);
    }

}